package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.staeconomiccooperative.entity.StaEconomicCooperative;
import com.jdjw.modules.staeconomiccooperative.mapper.StaEconomicCooperativeMapper;
import com.jdjw.modules.thirdpost.param.StaEconomicCooperativeParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class StaEconomicCooperativeImpl {
    @Autowired
    private static StaEconomicCooperativeMapper staEconomicCooperativeMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        StaEconomicCooperativeImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setStaEconomicCooperativeMapper(StaEconomicCooperativeMapper staEconomicCooperativeMapper) {
        StaEconomicCooperativeImpl.staEconomicCooperativeMapper = staEconomicCooperativeMapper;
    }


    public static void doPoststaEconomicCooperative(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 25;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 25;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 25;
        }
        try {
            //查询
            QueryWrapper entityWrapper = new QueryWrapper();
            if (StringUtils.isNotBlank(thirdPostInParm.getStartTime())) {
                entityWrapper.ge("create_time", thirdPostInParm.getStartTime());
            } else if (StringUtils.isNotBlank(thirdPostInParm.getEndTime())) {
                entityWrapper.le("create_time", thirdPostInParm.getEndTime());
            }
            List<StaEconomicCooperative> all = staEconomicCooperativeMapper.selectList(entityWrapper);
            if (all == null || all.size() < 1) {
                log.info("经合社编码基础表数据库查询数据为0条");
                return;
            }
            if (all.size() > 3000) {
                Collection<Collection<StaEconomicCooperative>> fgList = CommonUtil.subCollection(all, 2900);
                for (Collection<StaEconomicCooperative> obj : fgList) {
                    sendData((List<StaEconomicCooperative>) obj, url);
                }
            } else {
                sendData(all, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void sendData(List<StaEconomicCooperative> all, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (StaEconomicCooperative a : all) {
            //组装数据
            StaEconomicCooperativeParam param = new StaEconomicCooperativeParam();
            BeanUtils.copyProperties(a, param);
//            param.setId(a.getId());
            param.setId(a.getId());
            param.setBiz_id(a.getId());
            param.setCwh(a.getDept());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getDeptId(), 16));
            param.setJhsbh(a.getCooperativeNo());
            param.setJhsxydm(a.getCooperativeCode());

            param.setJhsmc(a.getCooperativeName());
            param.setDel_flag(String.valueOf(a.getDelFlag()));

            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("经合社基础表推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPoststaEconomicCooperative", "经合社基础表",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("经合社基础表推送失败", result);
            }
        } else {
            log.info("经合社基础表推送失败", "第三方返回空");
        }
    }
}


